<?php

class Employee extends CI_Controller {
 
       public function __construct()
    {
        parent::__construct();
        $this->load->model('employee_model');
    }
    
    public  function index(){
        if($this->session->userdata('is_logged_in')){
			$this->list_employee();
        }else{
        	$this->load->view('admin/login');	
        }
       
 
    }
    public function  list_employee(){
        $return_value = $this->employee_model->get_employee();
        $data['employee'] =$return_value['result'];
        $data['message'] =$return_value['message'];
        $data['state'] =$return_value['state'];
        $data['main_content'] = 'admin/employee/list';
        $this->load->view('includes/template', $data); 
    }
    
     public function add()
    {
        $this->load->model('department_model');
           date_default_timezone_set('America/Los_Angeles');
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
               $this->form_validation->set_rules('emp_id', 'Employee ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('birthday', 'Birthday', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('emp_level', 'Employee level', 'required');
            $this->form_validation->set_rules('tax_num', 'Tax number', 'required');
            $this->form_validation->set_rules('sal', 'Salary', 'required');
            $this->form_validation->set_rules('dept_id', 'Department ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
                 
            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                    'emp_id' => $this->input->post('emp_id'),
                    'name' => $this->input->post('name'),
                    'birthday' =>date('d-M-y',strtotime( $this->input->post('birthday'))),
                    'email' => $this->input->post('email'),          
                    'emp_level' => $this->input->post('emp_level'),
                    'tax_num' => $this->input->post('tax_num'),
                    'dept_id' => $this->input->post('dept_id'),
                    'sal' => $this->input->post('sal')
                );
               
                 
               $return_value = $this->employee_model->store_employee($data_to_store);
            
                 
              $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                    $this->session->set_flashdata('flash_message', 'added');
                    redirect('index.php/employee');
               } else {
                    //$this->session->set_flashdata('flash_message', 'not_added');
                    $this->session->set_flashdata('flash_message', 'not_added');
                    //redirect('index.php/employee/add');
                   redirect('index.php/employee');
               }
               
                
            }
        }
         $return_value = $this->department_model->get_department();
         $data['department'] =$return_value['result'];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
         $data['main_content'] = 'admin/employee/add';
         $this->load->view('includes/template', $data); 

        
    }
    public function update()
    {
           $this->load->model('department_model');
           date_default_timezone_set('America/Los_Angeles');
        //product id 
        $id = $this->uri->segment(3);
        //if save button was clicked, get the data sent via post
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
            //form validation
           //form validation
            $this->form_validation->set_rules('emp_id', 'Employee ID', 'required');
            $this->form_validation->set_rules('name', 'Name', 'required');
            $this->form_validation->set_rules('birthday', 'Birthday', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('emp_level', 'Employee level', 'required');
            $this->form_validation->set_rules('tax_num', 'Tax number', 'required');
            $this->form_validation->set_rules('sal', 'Salary', 'required');
            $this->form_validation->set_rules('dept_id', 'Department ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'emp_id' => $this->input->post('emp_id'),
                    'name' => $this->input->post('name'),
                    'birthday' =>date('d-M-y',strtotime( $this->input->post('birthday'))),
                    'email' => $this->input->post('email'),          
                    'emp_level' => $this->input->post('emp_level'),
                    'tax_num' => $this->input->post('tax_num'),
                    'dept_id' => $this->input->post('dept_id'),
                    'sal' => $this->input->post('sal')
                );
                //if the insert has returned true then we show the flash message
               
                
                  $data  = $data_to_store;

            
                
                
                $return_value = $this->employee_model->update_employee($data_to_store);
                
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/employee');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                    redirect('index.php/employee/update/'.$data_to_store['emp_id'].'');
               }
               
               
//           $data['info'] =$sql;
//           $data['main_content'] = 'test';
//           $this->load->view('includes/template', $data); 

            }//validation run

        }

        //employee data
         $return_value = $this->employee_model->get_employee_by_id($id);
         $array_employee =$return_value['result'];
         $data['employee'] = $array_employee[0];
         $data['message'] =$return_value['message'];
         $data['state'] =$return_value['state'];
    
         $return_value1 = $this->department_model->get_department();
         $data['department'] =$return_value1['result'];
        //load the view
        $data['main_content'] = 'admin/employee/edit';
        $this->load->view('includes/template', $data);            

    }//update
    
     /**
    * Delete product by his id
    * @return void
    */
    public function delete()
    {
        
        $id = $this->uri->segment(3);
          $return_value = $this->employee_model->delete_employee($id);
               $data['department'] =$return_value['result'];
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'deleted');
                    redirect('index.php/employee');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_deleted');
                    redirect('index.php/employee');
               }
    }//edit
    
    public function update_a_field()
    {
            $this->form_validation->set_rules('af_emp_field', 'Employee Field', 'required');
            $this->form_validation->set_rules('af_value', 'Value', 'required');
            $this->form_validation->set_rules('af_emp_id', 'Employee ID', 'required');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
    
                $data_to_store = array(
                    'af_emp_field' => $this->input->post('af_emp_field'),
                    'af_value' => $this->input->post('af_value'),
                    'af_emp_id' => $this->input->post('af_emp_id')
                );
                //if the insert has returned true then we show the flash message
               
             
                
//                              $data = $data_to_store;
//              $sql = "UPDATE ".$this->config->item('schema').".Employee
//                    SET 
//                      ".$data['af_emp_field']."=".$data['af_value']." "
//                       . "WHERE emp_id='".$data['af_emp_id']."'";
//             $data['info'] =$sql;    
//            $data['main_content'] = 'test';
//        $this->load->view('includes/template', $data); 
                              
              $return_value = $this->employee_model->update_af_in_employee($data_to_store);
                
               $data['message'] =$return_value['message'];
               $data['state'] =$return_value['state'];
               if($return_value['state']== 1){
                     $this->session->set_flashdata('flash_message', 'updated');
                    redirect('index.php/employee');
               } else {
                     $this->session->set_flashdata('flash_message', 'not_updated');
                     redirect('index.php/employee');
               }
            }
            else {
              $this->list_employee();
            }
        }
    
  
}

